<?php
namespace app\library\admin;
use app\admin\controller\Admin;
use app\library\model\Open as OpenModel;
// 开馆管理控制器
class Open extends Admin{

    public function index()
    {    
    	$this->assign('tab_type', 3);
        $data_list = OpenModel::where('')->select();
        $this->assign('data_list', $data_list);
      
        return $this->fetch();
    }
    /**
     * 添加闭馆时间
     * @return mixed
     */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            if ($data['starttime'] > $data['endtime']) {
                return $this->error('结束时间必须大于开始时间');
            }
            unset($data['id']);
            if (!OpenModel::create($data)) {
                return $this->error('添加失败！');
            }
            return $this->success('添加成功。');
        }
        return $this->fetch('form');
    }
    /**
     * 修改闭馆时间
     * @return mixed
     */
    public function edit($id=0){
    	$row = OpenModel::where('id', $id)->field('id,week,period,starttime,endtime')->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            if ($data['starttime'] > $data['endtime']) {
                return $this->error('结束时间必须大于开始时间');
            }
            if (!OpenModel::update($data)) {
                return $this->error('编辑失败！');
            }
            return $this->success('编辑成功。');
        }
        $this->assign('id',$id);
		$this->assign('data_info', $row);
        return $this->fetch('form');
    }
}